<template>
    <view>
        <uni-segmented-control :current="current" :values="items" @clickItem="onClickItem" styleType="text" activeColor="#0D7AF7"></uni-segmented-control>
        <view class="content">
            <view v-show="current === 0">
                <view  v-for="(item,index) in List1" :key="item.id">
					<view class="card">
						<view>{{item.title}}</view>
						<view>开始日期:{{item.time}}</view>
						<view>截止日期:{{item.endTime}}</view>
						<button class="btnLow" @click="under(item)">下架</button>
					</view>
				</view>
				<navigator class="active"  url="/pages/activity/releaseActivity/releaseActivity">+发布活动</navigator>
            </view>
			
            <view v-show="current === 1">
                <view  v-for="(item,index) in List2" :key="item.id">
                	<view class="card">
                		<view>{{item.title}}</view>
                		<view>开始日期:{{item.time}}</view>
                		<view>截止日期:{{item.endTime}}</view>
						<view class="status0" v-if="item.status==0">审核中</view>
						<view class="status1" v-if="item.status==1">审核通过</view>
						<view class="status2" v-if="item.status==2">审核失败</view>
                		<button class="btnLow" v-if="item.status==1" @click="on(item)">上架</button>
						<button class="btnLow" v-if="item.status!=1" @click="del(item)">删除</button>
                	</view>
                </view>
				<navigator class="active" url="/pages/activity/releaseActivity/releaseActivity">+发布活动</navigator>
            </view>
        </view>
    </view>
</template>
<script>
export default {
  data() {
    return {
        items: ['已上架', '未上架'],
        current: 0,
			List: [],
		 List1: [], // 用于存放status为3的数据  
		List2: [], // 用于存放status为0、1、2的数据  
    };
  },
  mounted() {
  	this.getActivity();
  },
  methods: {
	 // 获取指定店铺服务
	 getActivity(){
	 	// 获取token
	 	var token = uni.getStorageSync('Token');
	 	
	 	// 获取登录店铺的名字
	 	// 然后将赋值给请求体中的publisher用于获取店铺的发布信息
	 	var name =uni.getStorageSync('UserInfo').name;
	 	console.log(name)
	 	uni.request({
	 		 url: `http://localhost:7001/ruoyi-system/buying/list`,
	 		    method: 'GET',
	 			data: {
	 				'publisher': name
	 			},
	 		    header: {
	 		        'Authorization': 'Bearer ' + token
	 		    },
	 		success: (res) => {
	 			if (res.statusCode === 200) {
	 				console.log("res...............................");
	 				console.log(res.data.rows);
	 				this.List = res.data.rows ;
					console.log(this.List);
					this.List1 = this.List.filter(item => item.status === 3);  
					this.List2 = this.List.filter(item => [0, 1, 2].includes(item.status));   
					
	 			} else {
	 				reject(`Failed to fetch details. Status code: ${res.statusCode}`);
	 			}
	 		},
	 		fail: (err) => {
	 			reject(`Request failed: ${err.errMsg}`);
	 		}
	 	});
	 },
	 
	 
    onClickItem(e) {
      if (this.current != e.currentIndex) {
        this.current = e.currentIndex;
      }
    },
	
	// 下架
	under(item){
		console.log(item);
		uni.showModal({
		        title: '确认操作',
		        content: '确定要将该活动下架吗？',
		        success: (res) => {
		          if (res.confirm) {
		            // 用户点击确定,获取该活动id,并向服务器发送请求将状态修改为1
		            // 获取token
		            var token = uni.getStorageSync('Token');
		            
		            // 获取登录点击的活动的id
		            var id =item.id		            
		            uni.request({
		            	 url: `http://localhost:7001/ruoyi-system/buying`,
		            	    method: 'PUT',
		            		data: {
		            			'id': id,
								'status': 1
		            		},
		            	    header: {
		            	        'Authorization': 'Bearer ' + token
		            	    },
		            	success: (res) => {
		            		if (res.statusCode === 200) {
		            			console.log("res...............................");
		            			console.log(res.data.rows);  
		            			// 成功之后,刷新本页面
								uni.reLaunch({
								        url: '/pages/activity/activity' // 这里填写您想要跳转到的页面路径
								    });
		            		} else {
		            			reject(`Failed to fetch details. Status code: ${res.statusCode}`);
		            		}
		            	},
		            	fail: (err) => {
		            		reject(`Request failed: ${err.errMsg}`);
		            	}
		            });
		          } else if (res.cancel) {
		            // 用户点击取消
		            console.log('用户点击取消');
		          }
		        }
		      });
	},
	// 上架
	on(item){
		console.log(item);
		uni.showModal({
		        title: '确认操作',
		        content: '确定要将该活动上架吗？',
		        success: (res) => {
		          if (res.confirm) {
		            // 用户点击确定,获取该活动id,并向服务器发送请求将状态修改为3
		            // 获取token
		            var token = uni.getStorageSync('Token');
		            
		            // 获取登录点击的活动的id
		            var id =item.id		            
		            uni.request({
		            	 url: `http://localhost:7001/ruoyi-system/buying`,
		            	    method: 'PUT',
		            		data: {
		            			'id': id,
								'status': 3
		            		},
		            	    header: {
		            	        'Authorization': 'Bearer ' + token
		            	    },
		            	success: (res) => {
		            		if (res.statusCode === 200) {
		            			console.log("res...............................");
		            			console.log(res.data.rows);  
		            			// 成功之后,刷新本页面
								uni.reLaunch({
								        url: '/pages/activity/activity' // 这里填写您想要跳转到的页面路径
								    });
		            		} else {
		            			reject(`Failed to fetch details. Status code: ${res.statusCode}`);
		            		}
		            	},
		            	fail: (err) => {
		            		reject(`Request failed: ${err.errMsg}`);
		            	}
		            });
		          } else if (res.cancel) {
		            // 用户点击取消
		            console.log('用户点击取消');
		          }
		        }
		      });
	},
	// 删除
	del(item){
		console.log(item);
		uni.showModal({
		        title: '确认操作',
		        content: '确定要将该活动删除吗？',
		        success: (res) => {
		          if (res.confirm) {
		            // 用户点击确定,获取该活动id,并向服务器发送请求将该活动删除
		            // 获取token
		            var token = uni.getStorageSync('Token');
		            
		            // 获取登录点击的活动的id
		            var id =item.id		            
		            uni.request({
		            	 url: `http://localhost:7001/ruoyi-system/buying/${id}`,
		            	    method: 'DELETE',
		            	    header: {
		            	        'Authorization': 'Bearer ' + token
		            	    },
		            	success: (res) => {
		            		if (res.statusCode === 200) {
		            			console.log("res...............................");
		            			console.log(res.data.rows);  
		            			// 成功之后,刷新本页面
								uni.reLaunch({
								        url: '/pages/activity/activity' // 这里填写您想要跳转到的页面路径
								    });
		            		} else {
		            			reject(`Failed to fetch details. Status code: ${res.statusCode}`);
		            		}
		            	},
		            	fail: (err) => {
		            		reject(`Request failed: ${err.errMsg}`);
		            	}
		            });
		          } else if (res.cancel) {
		            // 用户点击取消
		            console.log('用户点击取消');
		          }
		        }
		      });
	},
  }
};
</script>
<style>

.card{
	margin-top: 15px;
	margin-left: 8px;
	width: 357px;
	height: 95px;
	background-color: rgba(255,255,255,1);
	border: 1px solid rgba(187,187,187,1);
	position: relative;
}
.card view:nth-child(1){
	margin-left: 17px;
	margin-top: 10px;
	width: 216px;
	height: 20px;
	line-height: 20px;
	color: rgba(16,16,16,1);
	font-size: 15px;
	text-align: left;
	font-family: PingFangSC-regular;
}
.card view:nth-child(2){
	margin-left: 18px;
	margin-top: 10px;
	top: 175px;
	width: 250px;
	height: 20px;
	line-height: 20px;
	color: rgba(106, 106, 106, 1.0);
	font-size: 14px;
	text-align: left;
	font-family: PingFangSC-regular;
}
.card view:nth-child(3){
	margin-left: 18px;
	margin-top: 5px;
	top: 175px;
	width: 250px;
	height: 20px;
	line-height: 20px;
	color: rgba(106, 106, 106, 1.0);
	font-size: 14px;
	text-align: left;
	font-family: PingFangSC-regular;
}
.status0{
	position: absolute;
	left: 280px;
	top: 15px;
	width: 62px;
	height: 20px;
	line-height: 20px;
	color: rgba(251,142,11,1);
	font-size: 14px;
	text-align: left;
	font-family: PingFangSC-regular;
}
.status1{
	position: absolute;
	left: 280px;
	top: 15px;
	width: 62px;
	height: 20px;
	line-height: 20px;
	color: rgba(129,179,55,1);
	font-size: 14px;
	text-align: left;
	font-family: PingFangSC-regular;
}
.status2{
	position: absolute;
	left: 280px;
	top: 15px;
	width: 62px;
	height: 20px;
	line-height: 20px;
	color: rgba(234,23,3,1);
	font-size: 14px;
	text-align: left;
	font-family: PingFangSC-regular;
}

.btnLow{
	position: absolute;
	left: 265px;
	top: 52px;
	width: 80px;
	height: 30px;
	line-height: 30px;
	border-radius: 4px;
	background-color: rgba(13,122,247,1);
	color: rgba(255,255,255,1);
	font-size: 14px;
	text-align: center;
	font-family: Roboto;
}
.active{
	/* position: absolute;
	left: 54px;
	top: 502px; */
	margin-left: 54px;
	margin-top: 30px;
	width: 267px;
	height: 41px;
	line-height: 41px;
	border-radius: 4px;
	background-color: rgba(251,142,11,1);
	color: rgba(255,255,255,1);
	font-size: 14px;
	text-align: center;
	font-family: Roboto;
}
</style>